home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 14 / lotuswks.zip / WSFF4.TXT < prev   
Text File  |  1986-07-29  |  20KB  |  544 lines

  1.                             WORKSHEET FILE FORMAT 
  2.                                   FROM LOTUS 
  3.  
  4.                       APPENDIX B - THE FORMULA COMPILER
  5.  
  6.                Copyright(c) 1984, Lotus Development Corporation 
  7.                                161 First Street 
  8.                         Cambridge, Massachusetts 02142 
  9.                                 (617) 492-7171 
  10.                       Electronic Edition, December, 1984 
  11.                              All Rights Reserved 
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       APPENDIX B:  The Formula Compiler 
  69.  
  70.  This appendix describes the internal workings of the formula compiler.  The
  71.  compiler transforms an ASCII string of characters representing a formula to
  72.  its Reverse Polish code.  The basic algorithm utilizes and SR parser (SR =
  73.  shift and reduce).  The aim of the parser is to apply a set of reduction
  74.  rules which embody the syntax of the compiler to an input string.  Formula
  75.  code is compiled to a temporary buffer. 
  76.  
  77.  Lexicon Analysis 
  78.  
  79.  A lexical analyzer breaks up the input string into lexical units called
  80.  tokens.  A token is a substring of the original input string operand,
  81.  operator, or special symbol (such as comma, parentheses, etc.) In addition,
  82.  the lexical analyser supplies two special tokens, "beginning of formula"
  83.  (boform) and "end of formula" (eoform), to facilitate the compilation
  84.  process.  The lexical analyzer identifies and processes literals (both
  85.  number and string), cell and range references, operators, and function
  86.  calls.  It assigns a unique code to each distinct operator, function, or
  87.  type of operand. 
  88.  
  89.  A function with no arguments is treated like a number. 
  90.  
  91.  Syntax Analysis 
  92.  
  93.  The syntactical analysis of a formula is accomplished by processing a list
  94.  of tokens in left-to-right order.  A stack called the syntax is also used
  95.  during the syntactical scan.  The basic algorithm is as follows: 
  96.  
  97.  Repeat the following steps: 
  98.  
  99.  1) Get the next token 
  100.  
  101.  2) If the token is a literal or cell reference: 
  102.        a) Push the number code on the syntax stack 
  103.        b) Push the number code on the syntax stack 
  104.  
  105.  3) If the token is a range reference: 
  106.        a) Compile code to push the range reference 
  107.        b) Push the range code on the syntax stack 
  108.  
  109.  4) Otherwise push the token code for the token on the syntax stack. 
  110.  
  111.  For each syntax rule, if the pattern on the top of the  syntax matches the
  112.  rule pattern take the action associated with the rule and start scanning
  113.  from the beginning for any additional rules which may apply. 
  114.  
  115.  When a token code is pushed on the syntax stack, an additional word of
  116.  zeros is also pushed on the stack.  This is used when compiling function
  117.  calls to hold the function's argument count. 
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  Rule Matching 
  124.  
  125.  A relatively small number of rules are used to process formulas of arbitrary
  126.  complexity.  If a rule matches the top of the syntax stack, then the
  127.  compiler takes a specific action and rule scanning starts again with the
  128.  first rule.  Each rule matches certain patterns on the syntax stack.  A
  129.  typical rule might be: if the top of the stack is the token for right
  130.  parenthesis, and the next-to-top is a number, and the second form the top
  131.  is a left parenthesis, then pop the top three items from the syntax stack
  132.  and push the number on the syntax stack. 
  133.  
  134.  This rule can be more succinctly represented as: 
  135.  
  136.                         Stack 
  137.  
  138.           Before                      After                 Action 
  139.           ) 
  140.           number 
  141.           (                           number                none 
  142.  
  143.  
  144.  
  145.  The Rules 
  146.  
  147.  
  148.  The following are the syntax rules used to process formulas.  Note that the
  149.  order of the rules is important.  The rules for compilation of operators
  150.  used additional tables which assign a precedence number and opcode to each
  151.  legal unary and binary operator.  Thus, for example, there is a single
  152.  token code for minus sign (-), but there are two opcodes one for unary
  153.  minus and one for binary minus.  In addition, these two operators, while
  154.  lexically identical, also have different precedence.  In general, operators
  155.  of higher precedence will be performed before operators of lower precedence
  156.  are performed left-to-right.  All special operators (boform, eoform,
  157.  parentheses, comma, etc.) are implicitly assigned a precedence of zero. 
  158.  
  159.  Rule 1  Termination test 
  160.  
  161.                   Stack 
  162.  
  163.          Before           After       Action 
  164.          eoform                       Output a return code to compile buffer 
  165.          number                       Return, indicating successful compile 
  166.          boform
  167.  
  168.  Rule 2  Function argument processing 
  169.  
  170.                  Stack 
  171.          Before          After       Action 
  172.          '                           Error if range argument illegal for 
  173.          number or range             function. 
  174.          (               (           Increment argument count on stack 
  175.          function        function 
  176.  
  177.  Rule 3  Process final function argument 
  178.  
  179.                  Stack 
  180.          Before         After        Action 
  181.          )                           Error if range argument illegal for 
  182.          number or range             function. 
  183.          (                           Increment argument count on stack 
  184.          function       number       Compile function opcode 
  185.                                      If list function, compile argument 
  186.                                      count; otherwise error is wrong 
  187.                                      argument count. 
  188.  
  189.  
  190.  
  191.  
  192.  Rule 4  Parenthesis removal 
  193.  
  194.                 Stack 
  195.         Before         After        Action 
  196.         )                           Compile parenthesis opcode 
  197.         number 
  198.         (              number 
  199.         operator       operator 
  200.  
  201.  
  202.  
  203.  Rule 5  Binary operators 
  204.  
  205.                Stack 
  206.         Before         After        Action 
  207.         op2                         If binary op<binary op, rule does 
  208.         number                      not match.  Otherwise, compile opcode 
  209.         op1            op2          for operator op1. 
  210.  
  211.  
  212.  Rule 6  Unary operators 
  213.  
  214.                Stack 
  215.         Before      After           Action 
  216.         op2                         I unary op<binary op, rule does 
  217.         number      op2             not match.  Otherwise, compile opcode. 
  218.         op1         number          for operator op 1. 
  219.  
  220.  
  221.  Rule 7  Error detection 
  222.  
  223.               Stack 
  224.        Before       After          Action 
  225.        eoform                      Return indicating unsuccessful compile 
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  Table 9   Operator Precedence Table 
  232.  
  233.  Operator              Unary Precedence       Binary Precedence 
  234.  +                             6                      4 
  235.  -                             6                      4 
  236.  *                            na                      5 
  237.  /                            na                      7 
  238.  ^                            na                      3 
  239.  =                            na                      3 
  240.  < >                          na                      3 
  241.  < =                          na                      3 
  242.  > =                          na                      3 
  243.  <                            na                      3 
  244.  >                            na                      3 
  245.  #and#                        na                      1 
  246.  #or#                         na                      1 
  247.  #not#                        2                      na 
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  Example: 
  260.  
  261.  Using the above rules, we can now see how a particular formula is
  262.  compiled.  Let us consider the following formula: 
  263.  
  264.                   3+5*6 
  265.  
  266.  This is broken up by the lexical analyzer into seven tokens. 
  267.  
  268.                   boform 
  269.                   3 
  270.                   + 
  271.                   5 
  272.                   * 
  273.                   6 
  274.                   eoform 
  275.  
  276.  The syntax scans proceed as follows until a matching rule is found: 
  277.  
  278.  Stack 
  279.  
  280.  boform           number         +            number 
  281.                   boform         number       + 
  282.                                  boform       number 
  283.                                               boform 
  284.  
  285.  Compile buffer 
  286.  
  287.                   push 3         push 3       push 3 
  288.                                               push 5 
  289.  
  290.  At this point, rule 5 is invoked, but since the precedence of boform is
  291.  zero, no action is taken. 
  292.  
  293.  Stack 
  294.  
  295.  *                number 
  296.  number           * 
  297.  +                number 
  298.  number           + 
  299.  boform           number 
  300.                   boform 
  301.  
  302.  Compile buffer 
  303.  
  304.  push 3           push 3 
  305.  push 5           push 5 
  306.                   push 6 
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  At this  point, since the binary precedence of + is lower than the binary
  315.  precedence of *, rule 5 does apply, and the opcode for * is compiled.  The
  316.  stack is reduced by replacing number * number by number and scan is made,
  317.  but no further rule applies. 
  318.  
  319.  
  320.  Stack 
  321.  
  322.  number          eoform 
  323.  +               number 
  324.  number          + 
  325.  boform          number 
  326.                  boform 
  327.  
  328.  Compile buffer 
  329.  
  330.  push 3          push 3 
  331.  push 5          push 5 
  332.  push 6          push 6 
  333.  
  334.  
  335.  
  336.  Rule 5 applies again, and the opcode for + is compiled, reducing the stack
  337.  to boform, number, eoform.  Rescanning finds a match on rule 1 which
  338.  compiles a return opcode and terminates.  The final compiled code is thus: 
  339.  
  340.  push 3 
  341.  push 5 
  342.  push 6 
  343.  * 
  344.  + 
  345.  return 
  346.  
  347.  A Note on the Decompiler 
  348.  
  349.  The algorithm for the formula decompiler was taken verbatim from: 
  350.  
  351.  Writing Interactive Compilers and Interpreters, P.J. Brown, John Wiley and
  352.  Sons, 1979.  See chapter 6.2.  The algorithm itself is described on pages
  353.  216 and 217. 
  354.  
  355.  This algorithm is also described in the following article. 
  356.  
  357.  More on the Re-creation of Source Code from Reverse Polish, P.J. Brown,
  358.  Software Practice and Experience, Vol 7, 545-551 (1977). 
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  WORKSHEET COLUMN DESIGNATORS 
  370.  
  371.  Most records within the 1-2-3 Condensed Worksheet format are specified
  372.  with column/row designators (for example, column 0, row 0 equals A1).  When
  373.  determining the column designator, the table below will help make
  374.  conversion easier. 
  375.  
  376.  
  377.  Column   Hex   Dec        Column   Hex   Dec        Column   Hex   Dec 
  378.    A       0     1           BA     34     52          DA     68    104 
  379.    B       1     1           BB     35     53          DB     69    105 
  380.    C       2     2           BC     36     54          DC     6A    106 
  381.    D       3     3           BD     37     55          DD     6B    107 
  382.    E       4     4           BE     38     56          DE     6C    108 
  383.    F       5     5           BF     39     57          DF     6D    109 
  384.    G       6     6           BG     3A     58          DG     6E    110 
  385.    H       7     7           BH     3B     59          DH     6F    111 
  386.    I       8     8           BI     3C     60          DI     70    112 
  387.    J       9     9           BJ     3D     61          DJ     71    113 
  388.    K       A    10           BK     3E     62          DK     72    114 
  389.    L       B    11           BL     3F     63          DL     73    115 
  390.    M       C    12           BM     40     64          DM     74    116 
  391.    N       D    13           BN     41     65          DN     75    117 
  392.    O       E    14           BO     42     66          DO     76    118 
  393.    P       F    15           BP     43     67          DP     77    119 
  394.    Q      10    16           BQ     44     68          DQ     78    120 
  395.    R      11    17           BR     45     69          DR     79    121 
  396.    S      12    18           BS     46     70          DS     7A    122 
  397.    T      13    19           BT     47     71          DT     7B    123 
  398.    U      14    20           BU     48     72          DU     7C    124 
  399.    V      15    21           BV     49     73          DV     7D    125 
  400.    W      16    22           BW     4A     74          DW     7E    126 
  401.    X      17    23           BX     4B     75          DX     7F    127 
  402.    Y      18    24           BY     4C     76          DY     80    128 
  403.    Z      19    25           BZ     4D     77          DZ     81    129 
  404.   AA      1A    26           CA     4E     78          EA     82    130 
  405.   AB      1B    27           CB     4F     79          EB     83    131 
  406.   AC      1C    28           CC     50     80          EC     84    132 
  407.   AD      1D    29           CD     51     81          ED     85    133 
  408.   AE      1E    30           CE     52     82          EE     86    134 
  409.   AF      1F    31           CF     53     83          EF     87    135 
  410.   AG      20    32           CG     54     84          EG     88    136 
  411.   AH      21    33           CH     55     85          EH     89    137 
  412.   AI      22    34           CI     56     86          EI     8A    138 
  413.   AJ      23    35           CJ     57     87          EJ     8B    139 
  414.   AK      24    36           CK     58     88          EK     8C    140 
  415.   AL      25    37           CL     59     89          EL     8D    141 
  416.   AM      26    38           CM     5A     90          EM     8E    142 
  417.   AN      27    39           CN     5B     91          EN     8F    143 
  418.   AO      28    40           CO     5C     92          EO     90    144 
  419.   AP      29    41           CP     5D     93          EP     91    145 
  420.   AQ      2A    42           CQ     5E     94          EQ     92    146 
  421.   AR      2B    43           CR     5F     95          ER     93    147 
  422.   AS      2C    44           CS     60     96          ES     94    148 
  423.   AT      2D    45           CT     61     97          ET     95    149 
  424.   AU      2E    46           CU     62     98          EU     96    150 
  425.   AV      2F    47           CV     63     99          EV     97    151 
  426.   AW      30    48           CW     64    100          EW     98    152 
  427.   AX      31    49           CX     65    101          EX     99    153 
  428.   AY      32    50           CY     66    102          EY     9A    154 
  429.   AZ      33    51           CZ     67    103          EZ     9B    155 
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  (CONTINUED)
  439.  
  440.  
  441.  
  442.  
  443.                Column   Hex    Dec         Column    Hex    Dec 
  444.  
  445.                  FA     9C     156           HA      DO     208 
  446.                  FB     9D     157           HB      D1     209 
  447.                  FC     9E     158           HC      D2     210 
  448.                  FD     9F     159           HD      D3     211 
  449.                  FE     AO     160           HE      D4     212 
  450.                  FF     A1     161           HF      D5     213 
  451.                  FG     A2     162           HG      D6     214 
  452.                  FH     A3     163           HH      D7     215 
  453.                  FI     A4     164           HI      D8     216 
  454.                  FJ     A5     165           HJ      D9     217 
  455.                  FK     A6     166           HK      DA     218 
  456.                  FL     A7     167           HL      DB     219 
  457.                  FM     A8     168           HM      DC     220 
  458.                  FN     A9     169           HN      DD     221 
  459.                  FO     AA     170           HO      DE     222 
  460.                  FP     AB     171           HP      DF     223 
  461.                  FQ     AC     172           HQ      EO     224 
  462.                  FR     AD     173           HR      E1     225 
  463.                  FS     AE     174           HS      E2     226 
  464.                  FT     AF     175           HT      E3     227 
  465.                  FU     BO     176           HU      E4     228 
  466.                  FV     B1     177           HV      E5     229 
  467.                  FW     B2     178           HW      E6     230 
  468.                  FX     B3     179           HX      E7     231 
  469.                  FY     B4     180           HY      E8     232 
  470.                  FZ     B5     181           HZ      E9     233 
  471.                  GA     B6     182           IA      EA     234 
  472.                  GB     B7     183           IB      EB     235 
  473.                  GC     B8     184           IC      EC     236 
  474.                  GD     B9     185           ID      ED     237 
  475.                  GE     BA     186           IE      EE     238 
  476.                  GF     BB     187           IF      EF     239 
  477.                  GG     BC     188           IG      FO     240 
  478.                  GH     BD     189           IH      F1     241 
  479.                  GI     BE     190           II      F2     242 
  480.                  GJ     BF     191           IJ      F3     243 
  481.                  GK     CO     192           IK      F4     244 
  482.                  GL     C1     193           IL      F5     245 
  483.                  GM     C2     195           IM      F6     246 
  484.                  GN     C3     195           IN      F7     247 
  485.                  GO     C4     196           IO      F8     248 
  486.                  GP     C5     197           IP      F9     249 
  487.                  GQ     C6     198           IQ      FA     250 
  488.                  GR     C7     199           IR      FB     251 
  489.                  GS     C8     200           IS      FC     252 
  490.                  GT     C9     201           IT      FD     253 
  491.                  GU     CA     202           IU      FE     254 
  492.                  GV     CB     203           IV      FF     255 
  493.                  GW     CC     204 
  494.                  GX     CD     205 
  495.                  GY     CE     206 
  496.                  GZ     CF     207 
  497.  
  498.  
  499.  
  500.  
  501.  ANALYSIS OF 1-2-3  WORKSHEET FILE 
  502.  
  503.  The worksheet shown below was created in 1-2-3 and saved to disk. 
  504.  
  505.  
  506.  
  507.                                               Key: 
  508.  
  509.                                               A2..A5 Named Range (code 11) 
  510.           EXAMPLE                                 A2: Label (code 15) 
  511.              100                                  A3: Integer (code 13) 
  512.             12.5                                  A4: Number (code 14) 
  513.             87.5                                  A5: Formula (+A3-A4) 
  514.                                                       (code 16) 
  515.  
  516.  
  517.  The example shown below is a partial hex dump of this worksheet file.  By 
  518.  reading each record header, you can determine the type of record you are
  519.  encountering.  The record header will also tell you the length of that
  520.  follows the header.  By analyzing the record header, you can read the
  521.  records you want and skip unrelated records. 
  522.  
  523.  
  524.     362B:0100                           06 00 08 00 00 00 00 00 00 00 
  525.     362B:0110        04 00 2F 00 01 00  01 02 00 01 00 FF 03 00 01 00 
  526.     362B:0120        00 04 00 01 00 00  05 00 01 00 FF 07 00 1F 00 00 
  527.     362B:0130        00 01 00 71 00 09  00 08 00 14 00 00 00 00 00 00 
  528.     362B:0140        00 00 00 00 00 00  00 04 00 04 00 48 00 00 0B 00 
  529.     362B:0150        18 00 54 45 53 54  00 00 00 00 00 00 00 00 00 00 
  530.     362B:0160        00 00 00 00 01 00  00 00 04 00 18 00 19 00 00 FF 
  531.     362B:0170        FF 00 00 FF FF 00  00 FF FF 00 00 FF FF 00 00 FF 
  532.     362B:0180 
  533.  
  534.  
  535.     362B:05C0 
  536.     362B:05D0        00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 
  537.     362B:05E0        00 00 00 00 00 00  00 00 00 00 00 00 00 00 00 00 
  538.     362B:05F0        00 00 00 00 71 71  01 00 0F 00 0E 00 FF 00 00 01 
  539.     362B:0600        00 27 45 58 41 4D  50 4C 45 00 0D 00 07 00 FF 00 
  540.     362B:0610        00 02 00 64 00 
  541.     362B:0620                           10 00 1B 00 FF 00 00 04 00 00 
  542.     362B:0630        00 00 00 00 E0 55  40 0C 00 01 00 80 FE BF 01 00 
  543.     362B:0640        80 FF BF 0A 03 
  544.